package com.qcby.demo.proxyaop.chain;

import com.qcby.demo.proxyaop.interceptor.Interceptor;
import com.qcby.demo.proxyaop.proxy.star.IAdvertisement;
import com.qcby.demo.proxyaop.proxy.star.JiaLing;

import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

/**
 * @className: ChainJdkDynamicProxy
 * @description: 责任链动态代理
 * @author: lxt
 * @create: 2021-05-12 16:36
 **/
public class ChainJdkDynamicProxy {


    public static void main(String[] args) {
        //实例化真实对象
        JiaLing jiaLing = new JiaLing();

        //实例化经纪人拦截器
        AgentInterceptor agentInterceptor = new AgentInterceptor();
        //实例化公司拦截器
        CompanyInterceptor companyInterceptor = new CompanyInterceptor();

        List<Interceptor> interceptorChain = new ArrayList<>();
        interceptorChain.add(agentInterceptor);
        interceptorChain.add(companyInterceptor);

        // 责任链生效
        Object proxy = proxyInterceptorAll(jiaLing,interceptorChain);
        IAdvertisement finalProxyObject = (IAdvertisement) proxy;


        finalProxyObject.display();

//        //把经纪人拦截器和被代理对象配置进代理逻辑类中
//        ChainInterceptorHandler chainInterceptorHandler = new ChainInterceptorHandler(jiaLing, agentInterceptor);
//
//        // 外层包装了经理人拦截器的代理对象
//        IAdvertisement iAdvertisement = (IAdvertisement) Proxy.newProxyInstance(jiaLing.getClass().getClassLoader()
//                , jiaLing.getClass().getInterfaces(), chainInterceptorHandler);
//
//
//
//        //  公司拦截器包装  代理之后的对象
//        ChainInterceptorHandler chainInterceptorHandler1 = new ChainInterceptorHandler(iAdvertisement, companyInterceptor);
//
//        // 生成新的代理对象
//        IAdvertisement iAdvertisement1 = (IAdvertisement) Proxy.newProxyInstance(iAdvertisement.getClass().getClassLoader()
//                , iAdvertisement.getClass().getInterfaces(), chainInterceptorHandler1);
//
//        iAdvertisement1.display();

//        // 拦截器链 =>添加拦截器
//        List<Interceptor> interceptorChain = new ArrayList<>();
//        interceptorChain.add(agentInterceptor);
//        interceptorChain.add(companyInterceptor);
//
//
//        Object proxy = jiaLing;
//        // 遍历拦截器链，进行层层代理
//        for (Interceptor interceptor : interceptorChain) {
//            //把经纪人拦截器和被代理对象配置进代理逻辑类中
//            ChainInterceptorHandler chainInterceptorHandler = new ChainInterceptorHandler(proxy, interceptor);
//            //产生了第一个代理对象proxy1
//            //对第一个代理对象proxy1再进行一次动态代理
//            proxy = Proxy.newProxyInstance(proxy.getClass().getClassLoader(), proxy.getClass().getInterfaces(), chainInterceptorHandler);
//        }
//        IAdvertisement finalProxyObject = (IAdvertisement) proxy;
//        //调用最后的代理对象的方法
//        finalProxyObject.display();

    }

    /**
     * 动态包装目标类
     * @return
     */
    public static Object proxyInterceptorAll(Object target,List<Interceptor> interceptorChain){
        Object proxy = target;
        for (Interceptor interceptor : interceptorChain) {
            //把经纪人拦截器和被代理对象配置进代理逻辑类中
            ChainInterceptorHandler chainInterceptorHandler = new ChainInterceptorHandler(proxy, interceptor);
            // 外层包装了经理人拦截器的代理对象
            proxy =  Proxy.newProxyInstance(proxy.getClass().getClassLoader()
                    , proxy.getClass().getInterfaces(), chainInterceptorHandler);
        }
        return proxy;
    }
}


